<template>
  <div class="new-message">
    <form @submit.prevent="addMessage">
      <label for="new-messag">New Message (enter to add):</label>
      <input type="text" name="new-message" v-model="newMessage" />
      <p class="red-text" v-if="feedback">{{feedback}}</p>
      <button class="btn waves-effect waves-light" type="submit" >
        Submit
        <i class="material-icons right">send</i>
      </button>
    </form>
  </div>
</template>

<script>
import db from "@/firebase/init.js";

export default {
  name: "NewMessage",
  props: ["name"],
  data() {
    return {
      newMessage: null,
      feedback: null
    };
  },
  methods: {
    addMessage() {
      if (this.newMessage) {
        //console.log(db)

        db.collection("messages")
          .add({
            content: this.newMessage,
            name: this.name,
            timestamp: Date.now()
          })
          .catch(err => {
            console.log(err);
          });
        this.newMessage = null;
        this.feedback = null;
      } else {
        this.feedback = "You must enter some message";
      }
    }
  }
};
</script>

<style lang="scss" scoped>
</style>